import request from '@/utils/request'
import store from '@/store'
// 查询generalUser列表
export function listgeneralUser(query) {
    return request({
        url: '/api/v1/generalUserList',
        method: 'get',
        params: query
    }).then(function (res) {
        
        let sexOptions = store.getters.parameter.sys_user_sex;
        let relationshipOptions = store.getters.parameter.sys_family_relationship;
        let usertypeOptions = store.getters.parameter.general_user_type;
        let boolOptions = store.getters.parameter.sys_bool;
        let userLevelOptions = store.getters.parameter.general_user_level;
        res.data.list.forEach((item, index) => {
            // console.log(item)
            for (let key in item) {
                // console.log(key + '---' + item[key])
                if (key == "familyRelationship") {
                    item[key] = getDictLabelByValue(item[key],relationshipOptions)
                }
                if (key == "sex") {
                    item[key] = getDictLabelByValue(item[key],sexOptions)
                }
                if (key == "userType") {
                    item[key] = getDictLabelByValue(item[key],usertypeOptions)
                }
                if (key == "isFamilyOwner") {
                    item[key] = getDictLabelByValue(item[key],boolOptions)
                }
                if (key == "userLevel") {
                    item[key] = getDictLabelByValue(item[key],userLevelOptions)
                }
                //isFamilyOwner

            }
             
            res.data.list[index] = item
        });
        // console.log(res.data.list)
        return res;

    })
}

function getDictLabelByValue(value,dict_list){
    let dictLabel="";
    if(dict_list){
        dict_list.forEach((_item,index)=>{
            if(_item.dictValue==value){
                dictLabel= _item.dictLabel
            }
        })
        return dictLabel;
    }else{
        console.log(value);
        return ;
    }
    
}

// 查询generalUser详细
export function getgeneralUser(userId) {
    return request({
        url: '/api/v1/generalUser/' + userId,
        method: 'get'
    })
}


// 新增generalUser
export function addgeneralUser(data) {
    return request({
        url: '/api/v1/generalUser',
        method: 'post',
        data: data
    })
}

// 修改generalUser
export function updategeneralUser(data) {
    return request({
        url: '/api/v1/generalUser',
        method: 'put',
        data: data
    })
}

// 删除generalUser
export function delgeneralUser(userId) {
    return request({
        url: '/api/v1/generalUser/' + userId,
        method: 'delete'
    })
}
